package org.ccay.manage.i18n.cluster.impl;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.async.AsyncMessage;
import org.ccay.async.IMessageProcessor;
import org.ccay.async.IMessageSender;
import org.ccay.core.exception.ApplicationException;
import org.ccay.i18n.events.II18nChangedEventArgs;
import org.ccay.i18n.events.II18nChangedHandler;
import org.ccay.manage.i18n.servlet.I18nSyncServlet;
import org.ccay.ws.service.IClusterInvokeService;

/**
 * 集群环境i18n国际化修改事件处理
 * @author chaowangbang
 *
 */
@Named
public class ClusterI18nChangedHandler implements II18nChangedHandler, IMessageProcessor{
	
	@Inject
	private IClusterInvokeService cluster; 	
	@Override
	public void process(AsyncMessage message) throws ApplicationException {
		cluster.invoke("servlet/i18nSync", null);
	}

	@Inject
	private IMessageSender messageSender;
	@Override
	public void execute(Object sender, II18nChangedEventArgs args) throws ApplicationException {
		if(sender.getClass().equals(I18nSyncServlet.class)){
			//忽略来自servlet的同步更新，避免循环触发
			return;
		}
		AsyncMessage message = new AsyncMessage("I18nSync");
		message.setContent(args);
		//使用异步框架处理此事件
		messageSender.send(message);
	}

}
